package com.corner.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.corner.R;
import com.corner.model.cornerservice.domain.Like;
import com.corner.ui.activity.ProfileActivity;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * Created by Administrator on 2015/5/5.
 */
public class IntersectionAdapter extends BaseAdapter implements View.OnClickListener {
    List<Like> likes;
    Context context;

    public IntersectionAdapter(Context context, List<Like> likes) {
        this.context = context;
        this.likes = likes;
    }

    @Override
    public int getCount() {
        return likes.size();
    }

    @Override
    public Object getItem(int position) {
        return likes.get(position % likes.size());
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = new SimpleDraweeView(context);
            int width_height = context.getResources().getDimensionPixelSize(R.dimen.dimen_avater_width_height);
            GridView.LayoutParams param = new GridView.LayoutParams(width_height, width_height);
            convertView.setLayoutParams(param);
            ((SimpleDraweeView) convertView).setScaleType(ImageView.ScaleType.FIT_XY);
            GenericDraweeHierarchyBuilder builder =
                    new GenericDraweeHierarchyBuilder(context.getResources());
            RoundingParams roundingParams = new RoundingParams();
            roundingParams.setRoundAsCircle(true);
            GenericDraweeHierarchy hierarchy = builder
                    .setFadeDuration(300)
                    .setRoundingParams(roundingParams)
                    .setPlaceholderImage(context.getResources().getDrawable(R.drawable.common_avatar))
                    .build();
            ((SimpleDraweeView) convertView).setHierarchy(hierarchy);
        }
        String url = (likes.get(position).getUser()).getAvaterUrl();
        if (url != null) {
            ((SimpleDraweeView) convertView).setImageURI(Uri.parse(url));
        }
        convertView.setOnClickListener(this);
        convertView.setTag(likes.get(position).getUser().getObjectId());
        return convertView;
    }

    @Override
    public void onClick(View v) {
        if (v.getTag() != null) {
            Long userId = (Long) v.getTag();
            Intent intent = new Intent(context, ProfileActivity.class);
            intent.putExtra(ProfileActivity.USER_ID, userId);
            context.startActivity(intent);
        }
    }
}
